package com.xinxin.blockchain.asset.template;

import com.xinxin.blockchain.asset.enums.AssetBusinessTypeEnum;
import com.xinxin.blockchain.asset.strategy.AssetFrozenOperateStrategy;
import com.xinxin.blockchain.model.AccountAsset;
import com.xinxin.blockchain.model.AccountAssetRecord;
import lombok.AccessLevel;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 资产冻结
 *
 * @author ys
 * @date 2021/5/24
 */
@Component
public class FrozenAssetTemplate extends AbstractAssetTemplate {

    @Getter(AccessLevel.PROTECTED)
    @Autowired
    private AssetFrozenOperateStrategy operateStrategy;

    @Override
    protected AccountAssetRecord record(AccountAsset asset, BigDecimal amount, AssetBusinessTypeEnum businessType) {
        final BigDecimal available = asset.getAvailable();
        final BigDecimal frozen = asset.getFrozen();
        final BigDecimal total = asset.getTotal();

        return new AccountAssetRecord()
                .setAccountId(asset.getAccountId())
                .setAssetId(asset.getId())
                .setAssetType(asset.getType())
                .setAssetTypeText(asset.getTypeText())
                .setBusinessType(businessType.getType())
                .setBusinessTypeText(businessType.getDesc())
                .setAmount(amount)
                .setBeforeAvailable(available)
                .setAfterAvailable(available.subtract(amount))
                .setBeforeFrozen(frozen)
                .setAfterFrozen(frozen.add(amount))
                .setBeforeTotal(total)
                .setAfterTotal(total);
    }
}
